package cn.com.bluemoon.daps.sync.core.sync;

import cn.com.bluemoon.daps.common.sql.IDbDataSource;
import cn.com.bluemoon.daps.model.entity.DapDataModelSubordinateKey;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.entity.DapDataModelTablePartition;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.google.common.collect.Lists;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据集入口类
 *
 * @author Jarod.Kong
 * @date 2021/1/15 9:00
 */
@Data
public class TableModel implements IDbDataSource {

    /**
     * 源数据
     */
    private DapDataModelTable table;

    private List<DapDataModelTableField> fields = Lists.newArrayList();

    private List<DapDataModelSubordinateKey> indexes = Lists.newArrayList();

    private List<DapDataModelTablePartition> partitions = Lists.newArrayList();
    /**
     * 建表语句 整合创建表、创建索引
     */
    private String createTableSql;
    /**
     * 独立创建表、创建索引
     */
    private List<String> createTableSqls;

    /**
     * 对比装载
     */
    private Tuple2<DapDataModelTable> v1AndV2DbTable = new Tuple2<>();
    private List<Tuple2<DapDataModelTableField>> v1AndV2DbFields = Lists.newArrayList();
    private List<Tuple2<DapDataModelSubordinateKey>> v1AndV2DbIndexes = Lists.newArrayList();
    private List<Tuple2<DapDataModelTablePartition>> v1AndV2DbPartitions = Lists.newArrayList();

    /**
     * 获取尝试格式化的建表语句
     * @return sql
     */
    public String getCreateTableSqlPretty() {
        if (createTableSql == null) {
            return null;
        }
        String sql;
        try {
            sql = SQLUtils.format(createTableSql, DbType.mysql);
        }catch (Exception e){
            // ignore
            sql = createTableSql;
        }
        return sql;
    }

    /**
     * 定义分组key，进行分析
     *
     * @return key
     */
    @Override
    public String getBmKey() {
        return getTable().getBmKey();
    }

    @Override
    public void setBmKey(String id) {
        if (getTable() != null) {
            getTable().setBmKey(id);
        }
    }

    /**
     * 针对对比组合后的字段列表进行升序排序
     *
     * @return 有序rank
     */
    public List<Tuple2<DapDataModelTableField>> getV1AndV2DbFieldsSortByRank() {
        List<Tuple2<DapDataModelTableField>> sort = getV1AndV2DbFields().stream().sorted((o1, o2) -> {
            if (o1 == null) return -1;
            if (o2 == null) return -1;
            if (o1.getNewT() == null || o1.getNewT().getRank() == null) {
                return -1;
            }
            if (o2.getNewT() == null || o2.getNewT().getRank() == null) {
                return -1;
            }
            if (o1.getNewT() != null && o2.getNewT() != null) {
                return o1.getNewT().getRank().compareTo(o2.getNewT().getRank());
            }
            if (o1.getOldT() == null || o1.getOldT().getRank() == null) {
                return -1;
            }
            if (o2.getOldT() == null || o2.getOldT().getRank() == null) {
                return -1;
            }
            if (o1.getOldT() != null && o2.getOldT() != null) {
                return o1.getOldT().getRank().compareTo(o2.getOldT().getRank());
            }
            return -1;
        }).collect(Collectors.toList());
        return sort;
    }

    public Map<String, DapDataModelTableField> getFieldNameAndFields() {
        return fields.stream().collect(Collectors.toMap(DapDataModelTableField::getFieldName, a -> a, (a, b) -> b));
    }

    public Map<String, DapDataModelSubordinateKey> getIndexNameAndIndexes() {
        return indexes.stream().collect(Collectors.toMap(DapDataModelSubordinateKey::getKeyName, a -> a, (a, b) -> b));
    }
    public Map<String, DapDataModelTablePartition> getPartitionFieldNameAndPartitions() {
        return partitions.stream().collect(Collectors.toMap(DapDataModelTablePartition::getPartitionFieldName, a -> a, (a, b) -> b));
    }
}
